package edu.istu.asu07_1.live.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.sun.org.apache.xalan.internal.xsltc.dom.LoadDocument;

import edu.istu.asu07_1.live.configs.Constants;

/**
 * Главная форма программы. Сразу после инициализации
 * выводится панель с редактированием настроек, после
 * этого запускается игровой движок и открывается перспектива
 * с игровым полем
 * @author romanches
 *
 */
public class MainForm extends JFrame implements ActionListener{

	private JComboBox fieldXSize;
	private JComboBox fieldYSize;
	private JComboBox defaultAgentsEnergy;

	private JComboBox initialHerbCount;
	private JComboBox herbEnergyDecriment;
	private JComboBox herbLiveTime;
	private JComboBox maxHerbCount;

	private JComboBox initialHerbivoreCount;
	private JComboBox herbivoreFeelArea;
	private JComboBox herbivoreEnergyDecriment;

	private JComboBox initialPredatorCount;
	private JComboBox predatorFeelArea;
	private JComboBox predatorEnergyDecriment;

	private JSlider timePeriodSlider;

	public MainForm() {
		init();
		setLocationOnScreenCenter();
		setLookAndFeel();
		setVisible(true);
	}

	private void init(){

		setTitle("Artificial live");
		setMinimumSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		buildPanels();
		loadSettings();
	}

	private void setLocationOnScreenCenter(){
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension screenSize = toolkit.getScreenSize();
		setLocation((screenSize.width/2)-DEFAULT_WIDTH/2, (screenSize.height/2)-DEFAULT_HEIGHT/2);
	}

	private void setLookAndFeel(){
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void buildPanels(){
		CellConstraints cc = new CellConstraints();

		FormLayout optionsLayout = new FormLayout(
				"10px, fill:p:grow,100px, 10px",
				"10px, fill:p:grow, 30px, 10px"
		);
//		DefaultFormBuilder optionsBuilder = new DefaultFormBuilder(optionsLayout, new FormDebugPanel());
		DefaultFormBuilder optionsBuilder = new DefaultFormBuilder(optionsLayout);

		FormLayout smallPanellayout = new FormLayout(
				"3px, 30px, 80px, 3px, 70px, 10px, 80px, 3px, 70px, fill:p:grow, 3px",
				"3px, 15px, 8px, 20px, 3px, 20px, 8px, 20px, 8px, 20px, 8px, 20px, 8px, 20px, " +
				"3px, 20px, 3px, 20px, 3px, 20px, 8px,20px,8px, 20px,3px, 20px, 3px, 20px,8px, 20px," +
				"8px, 20px,3px, 20px, 3px, 20px, fill:p:grow, 3px"
		);

//		DefaultFormBuilder smallPanelBuilder = new DefaultFormBuilder(smallPanellayout, new FormDebugPanel());
		DefaultFormBuilder smallPanelBuilder = new DefaultFormBuilder(smallPanellayout);
		smallPanelBuilder.addSeparator("Общие настройки окружающей среды", cc.xyw(2, 2, 9));

		smallPanelBuilder.addLabel("ширина", cc.xy(3, 4));
		fieldXSize = genComboBox(15, 30);
		smallPanelBuilder.add(fieldXSize, cc.xy(5, 4));

		smallPanelBuilder.addLabel("Скорость", cc.xy(7, 4));
		timePeriodSlider = new JSlider(0, 500);
		smallPanelBuilder.add(timePeriodSlider, cc.xy(9, 4));

		smallPanelBuilder.addLabel("высота", cc.xy(3, 6));
		fieldYSize = genComboBox(15, 25);
		smallPanelBuilder.add(fieldYSize, cc.xy(5, 6));

		smallPanelBuilder.addSeparator("Начальные настройки агентов", cc.xyw(2, 8, 9));

		smallPanelBuilder.addLabel("энергия", cc.xy(3, 10));
		defaultAgentsEnergy = genComboBox(50, 80);
		smallPanelBuilder.add(defaultAgentsEnergy, cc.xy(5, 10));

		smallPanelBuilder.addSeparator("Растения", cc.xyw(2, 12, 9));

		smallPanelBuilder.addLabel("Начальное количество", cc.xyw(3, 14, 3));
		initialHerbCount = genComboBox(1, 10);
		smallPanelBuilder.add(initialHerbCount, cc.xy(7, 14));

		smallPanelBuilder.addLabel("Max - количество", cc.xyw(3, 16, 3));
		maxHerbCount = genComboBox(10, 50);
		smallPanelBuilder.add(maxHerbCount, cc.xy(7, 16));

		smallPanelBuilder.addLabel("Время жизни растения", cc.xyw(3, 18, 3));
		herbLiveTime = genComboBox(10, 50);
		smallPanelBuilder.add(herbLiveTime, cc.xy(7, 18));

		smallPanelBuilder.addLabel("Приращение энергии", cc.xyw(3, 20, 3));
		herbEnergyDecriment = genComboBox(1, 20);
		smallPanelBuilder.add(herbEnergyDecriment, cc.xy(7, 20));

		smallPanelBuilder.addSeparator("Травоядные", cc.xyw(2, 22, 9));

		smallPanelBuilder.addLabel("Начальное количество", cc.xyw(3, 24, 3));
		initialHerbivoreCount = genComboBox(1, 10);
		smallPanelBuilder.add(initialHerbivoreCount, cc.xy(7, 24));

		smallPanelBuilder.addLabel("Область чувствит-ти", cc.xyw(3, 26, 3));
		herbivoreFeelArea = genComboBox(1, 3);
		smallPanelBuilder.add(herbivoreFeelArea, cc.xy(7, 26));

		smallPanelBuilder.addLabel("Убывание энергии", cc.xyw(3, 28, 3));
		herbivoreEnergyDecriment = genComboBox(1, 5);
		smallPanelBuilder.add(herbivoreEnergyDecriment, cc.xy(7, 28));

		smallPanelBuilder.addSeparator("Хищники", cc.xyw(2, 30, 9));

		smallPanelBuilder.addLabel("Начальное количество", cc.xyw(3, 32, 3));
		initialPredatorCount = genComboBox(1, 10);
		smallPanelBuilder.add(initialPredatorCount, cc.xy(7, 32));

		smallPanelBuilder.addLabel("Область чувствит-ти", cc.xyw(3, 34, 3));
		predatorFeelArea = genComboBox(1, 3);
		smallPanelBuilder.add(predatorFeelArea, cc.xy(7, 34));

		smallPanelBuilder.addLabel("Убывание энергии", cc.xyw(3, 36, 3));
		predatorEnergyDecriment = genComboBox(1, 5);
		smallPanelBuilder.add(predatorEnergyDecriment, cc.xy(7, 36));

		optionsBuilder.add(smallPanelBuilder.getPanel(), cc.xyw( 2, 2, 2));
		JButton accept = new JButton("Начать");
		accept.addActionListener(this);
		optionsBuilder.add(accept, cc.xy(3, 3));
		add(optionsBuilder.getPanel());
	}

	public void actionPerformed(ActionEvent arg0) {
		saveSettings();
		GameForm f = new GameForm();
		this.dispose();
	}
//	загрузка настроек по-умолчанию
	private void loadSettings(){
		fieldXSize.setSelectedIndex(15);
		fieldYSize.setSelectedIndex(10);

		initialHerbCount.setSelectedIndex(3);
		maxHerbCount.setSelectedIndex(30);
		herbLiveTime.setSelectedIndex(10);
		herbEnergyDecriment.setSelectedIndex(9);

		initialHerbivoreCount.setSelectedIndex(3);
		herbivoreFeelArea.setSelectedIndex(1);

		initialPredatorCount.setSelectedIndex(1);
		predatorEnergyDecriment.setSelectedIndex(1);
		predatorFeelArea.setSelectedIndex(2);
	}

	private void saveSettings(){
		Constants.FIELD_WIDTH = (Integer) fieldXSize.getSelectedItem();
		Constants.FIELD_HEIGHT = (Integer) fieldYSize.getSelectedItem();
		Constants.MODELATING_TIME_PERIOD = 1000 - timePeriodSlider.getValue();
		Constants.DEFAULT_AGENT_ENERGY = (Integer) defaultAgentsEnergy.getSelectedItem();

		Constants.INITIAL_HERB_COUNT = (Integer) initialHerbCount.getSelectedItem();
		Constants.MAX_HERB_COUNT = (Integer) maxHerbCount.getSelectedItem();
		Constants.HERB_ENERGY_DECRIMENT = (Integer) herbEnergyDecriment.getSelectedItem();
		Constants.HERB_LIVE_TIME = (Integer) herbLiveTime.getSelectedItem();

		Constants.INITIAL_HERBIVORE_COUNT = (Integer) initialHerbivoreCount.getSelectedItem();
		Constants.HERBIVORE_ENERGY_DECRIMENT = (Integer) herbivoreEnergyDecriment.getSelectedItem();
		Constants.HERBIVORE_FEEL_AREA = (Integer) herbivoreFeelArea.getSelectedItem();

		Constants.INITIAL_PRADATOR_COUNT = (Integer) initialPredatorCount.getSelectedItem();
		Constants.PREDATOR_ENERGY_DECRIMENT = (Integer) predatorEnergyDecriment.getSelectedItem();
		Constants.PREDATOR_FEEL_AREA = (Integer) predatorFeelArea.getSelectedItem();
	}

	private JComboBox genComboBox(int firstValue, int secondValue){
		JComboBox temp = new JComboBox();
		for(int i = firstValue; i<=secondValue; i++){
			temp.addItem(i);
		}
		return temp;
	}

	public static final int DEFAULT_WIDTH = 400;
	public static final int DEFAULT_HEIGHT = 550;
	public static final String OPTIONS_PANEL = "options";
	public static final String GAME_FIELD_PANEL = "game_field";
}
